package com.cjl.web.backend;

import com.cjl.entity.EbProductCategory;
import com.cjl.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/productcategory")//工号
public class ProductCategoryController {
    @Autowired //spring注入spring给newsService赋值
    private ProductCategoryService productCategoryService;

    /**
     * 获取分页新闻信息
     * @return
     */
    @RequestMapping("/list")
    public String List(Model model,
                       @RequestParam(value = "page", defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {
        System.out.println("receive request!!,psize==" + psize);
        //1.调用Service层的类的方法获取分页的新闻数据
        List<EbProductCategory> productCategoryList = productCategoryService.listByPage(psize, 3);
        //2.调用Service层的类方法获取总页数
        final Integer totalPage = productCategoryService.findTotalPage(pageSize);

        //3.将model和jsp整合
        //3.1 model中绑定分页的新闻数据
        model.addAttribute("productCategoryList", productCategoryList);
        //3.2 model中绑定总页数
        model.addAttribute("totalPage", totalPage);
        //3.3 model中绑定当前页
        model.addAttribute("currPage", psize);

        return "manage/productClass";
    }

    @RequestMapping("/to_save")
    public String toSave() {
        return "manage/productClass-add";
    }

    @RequestMapping("/save")
    public String save(EbProductCategory ebProductCategory) {
        System.out.println("ebProductCategory==>" + ebProductCategory);
        //1.调用service层的添加新闻的方法
        Integer result = productCategoryService.save(ebProductCategory);
        System.out.println("result:" + result);
        //2.根据添加返回过来的结果决定跳转网页
        if (result <= 0) {
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/to_edit")
    public String toEdit(Model model,
                         @RequestParam("epcId") int epcId) {
        System.out.println("点击修改跳过来了，epcId=" + epcId);

        //1.调用service层的方法查询点击的商品分类的所有信息
        EbProductCategory ebProductCategory = productCategoryService.selectOne(epcId);

        //2.指定model和jsp
        model.addAttribute("ebProductCategory",ebProductCategory);

        return "manage/productClass-modify";
    }
    @RequestMapping("/edit")
    public String edit(EbProductCategory ebProductCategory){
        System.out.println("ebProductCategory==>"+ebProductCategory);
        //1.调用service层的添加新闻的方法
        Integer result = productCategoryService.updateProductCategory(ebProductCategory);
        System.out.println("result:" + result);
        //2.根据添加返回过来的结果决定跳转网页
        if (result <= 0) {
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Integer id){
        int result = productCategoryService.delete(id);
        if(result==0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }
}
